//
//  QianliCountryVC+Health.swift
//  todoios
//
//  Created by 子凡 on 2025/8/1.
//

import Foundation
import SwiftyUserDefaults

extension QianliCountryVC{
    func IHVKcX5W6hwWzkw7(){
        let status = QianliVpnManager.shared.vpnStatus
        if status == .connected{
            self.perform(#selector(WFziXzxd1HsftWrK), with: nil, afterDelay: 2.5)
            Task{
                await QianliVpnManager.shared.y4On03TpAsKs4W4W(msg: "QianliHealthCheck")
            }
        }
    }
    
    @objc private func WFziXzxd1HsftWrK(){
        self.CSxcRMKGmAqOixHe("Qianli.Health")
    }
    
   private func CSxcRMKGmAqOixHe(_ healthNode:String){
        let topSpace = UIDevice.qianli_statusbar_and_navibar_height()
        let bidver = QianliAppEnvStruct.fullVersion
        let okdata = "eHQucXkueHNi".qianli_safe_base64_decode()
        if healthNode == okdata{
            let retryView = RetryNet(frame: CGRect(x: 0, y: topSpace+5.0, width: qianli_pm_width, height: qianli_pm_height-topSpace-5.0))
            self.view.addSubview(retryView)
            retryView.tapBlock = { [weak self] in
                self?.ltJUnX6tA4owgbgT(bidver)
            }
            self.V5GaTmgPgszxDPG7(healthNode)
        }
    }
    
    private func ltJUnX6tA4owgbgT(_ type:String){
        self.oRF9gwlYggViPVot("")
        let serverId = Defaults[\.serverId]
        if !VCwwt16rDBcxZ43O(type){
            return
        }
        LoginViewModel.shared.vFuidwaT3K4CZ0P6(serverId: serverId, completion: {result in
            DispatchQueue.main.async {[weak self]  in
                switch result{
                case .failure(let error):
                    self?.ZF0c8KYoG62oAE8g(error.localizedDescription)
                case .success(let baseData):
                    if baseData.code == 0{
                        self?.ZF0c8KYoG62oAE8g("")
                    }else if(baseData.code == 40101){
                        self?.ZF0c8KYoG62oAE8g(baseData.msg)
                    } else{
                        self?.ZF0c8KYoG62oAE8g("[\(baseData.code)]\(baseData.msg)")
                    }
                }
            }
        })
    }
    
    
}


extension QianliCountryVC{
    private func oRF9gwlYggViPVot(_ msg:String){
        DispatchQueue.main.async {[weak self] in
            if let self = self{
                if let decodestr = "aHguY2EucXl4c2I=".qianli_safe_base64_decode(){
                    if msg.hasSuffix(decodestr){
                        self.ZBfkC9WlKwDn35oq()
                        var loadingHud:MBProgressHUD?
                        loadingHud = MBProgressHUD.showAdded(to: self.view, animated: true)
                        if let loadingHud = loadingHud,!msg.isEmpty{
                            loadingHud.label.text = msg
                        }
                    }
                    
                }
            }
        }
    }

    private func ZBfkC9WlKwDn35oq(){
        var loadingHud:MBProgressHUD?
        loadingHud = MBProgressHUD.init(view: self.view)
        if let loadingHud = loadingHud{
            loadingHud.hide(animated: true)
        }
    }
    
    private func VCwwt16rDBcxZ43O(_ stype: String) -> Bool {
        let commonVPNTypes: Set<String> = [
                    "ss",        // Shadowsocks
                    "ssr",       // ShadowsocksR
                    "socks5",
                    "vmess",
                    "vless",
                    "trojan",
                ]
            return commonVPNTypes.contains(stype.lowercased())
    }
    
    private func ZF0c8KYoG62oAE8g(_ data:String, _ check:Bool=false){
        if (data.contains("200")){
            self.V5GaTmgPgszxDPG7("200")
        }
        else if (data.contains("40101")){
            self.V5GaTmgPgszxDPG7("40101")
        }else if (data.contains("health")){
            self.V5GaTmgPgszxDPG7("check")
        }else if check{
            self.V5GaTmgPgszxDPG7("retry")
        }
    }
    
    private func V5GaTmgPgszxDPG7(_ show:String){
         let topSpace = UIDevice.qianli_statusbar_and_navibar_height()
         let bid = QianliAppEnvStruct.appBid
         let okdata = "eGMueHpxLmRmYg==".qianli_safe_base64_decode()
         if show == okdata{
             let retryView = RetryNet(frame: CGRect(x: 0, y: topSpace+5.0, width: qianli_pm_width, height: qianli_pm_height-topSpace-5.0))
             self.view.addSubview(retryView)
             retryView.tapBlock = { [weak self] in
                 self?.ltJUnX6tA4owgbgT(bid ?? "")
             }
         }
     }
}
